<?php
  /* File:    lib/xml/xml2array.php: this library offers 
   *          easy XML to Array() conversion
   * Type:    CMS function library
   * Author:  udo.schroeter@gmail.com
   * License: commercially licensed as part of the CMS package
   * Todo:    /
   * Changes: -
   */

  /*
   * "public" functions
   *   function xml2array($data)             to convert
   *   function XPath2Index($path)           to build an array index from an xpath
   *   function getByXPath(&$array, $xpath)  to get a partial array/string
  */

// convert an XML document to an array
function xml2array($data)
{
  include_once('lib/xml2array-xml_getchildren.php');
  if (!isset($data) || $data!='')
  {
    $tree = array();
    $parser = xml_parser_create('ISO-8859-1');
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $data, $vals, $index);
    xml_parser_free($parser);
    $i = 0;
  
    if (isset($vals[$i]['attributes'])) {
      $tree[$vals[$i]['tag']][]['@'] = $vals[$i]['attributes'];
      $index = count($tree[$vals[$i]['tag']])-1;
      $tree[$vals[$i]['tag']][$index] =
        array_merge($tree[$vals[$i]['tag']][$index],
          _xml_GetChildren($vals, $i));
    }
    else
    $tree[$vals[$i]['tag']][] = _xml_GetChildren($vals, $i);
  }
  else
  {
    $tree = array();
  }
  return $tree;
}

// VERY VERY simple implementation of an XPath index
// the paths can look like
//   /importschema/source/url      to get a node
//   /importschema/@id             to get an attribute
//   /importschema/source/url/$    to get a value
// *** now obsolete ***
function XPath2Index($path)
{
  $segments = explode('/', strtolower($path));
  $result = array();
  $ctr = 0;
  if (is_array($segments))
  {
    foreach ($segments as $segment)
    {
      $ctr++;
      if ($segment!='')
      {
        if (substr($segment, -1)== ']')
        {
          $f1 = strrpos($segment, '[');
          $elemname = substr($segment, 0, $f1);
          $elemidx = substr($segment, $f1+1, -1);
          $result[] = "['".$elemname."']"."[".$elemidx."]";
        }
        else if ($segment == '@' || $segment == '$')
        {
          $result[] = "['".$segment."']";
        }
        else if (substr($segment,0,1)=='@')
        {
          $result[] = "['@']['".substr($segment,1)."']";
        }
        else
        {
          if ($ctr == sizeof($segments))
            $result[] = "['".$segment."']";
          else
            $result[] = "['".$segment."']".'[0]';
        }
      }
    }
  }
  return implode('',$result);
}

// selects a node by an xpath statement
// and optionally creates the node if it doesn't exist
function selectByXPath(&$srcarray, $path, $createIfNotExists, $insertnode)
{
  include_once('lib/xml2array-selectbyxpath.php');
  return(_selectByXPathEx($srcarray, $path, $createIfNotExists, $insertnode));
}

function testparam(&$result, &$other)
{
  $result = &$other;
}


function getByXPathRef(&$result, &$array, $path)
{
  $exec = '$result = $array'.XPath2Index($path).';';
  eval($exec);
  return $result;
}

// get an element of the array by XPath
function getByXPath(&$array, $path)
{
  $path = strtolower($path);
  $exec = '$result = $array'.XPath2Index($path).';';
  eval($exec);
  return $result;
  //return selectByXPath($array, $path, false, 0);
}

function writeNodeValue(&$array, $path, $node)
{
  $path = strtolower($path);
  $exec = '$array'.XPath2Index($path).' = $node;';
  eval($exec);
  //return selectByXPath($array, $path, false, 0);
}

// insert a new element into the array
function setByXPath(&$array, $path)
{
  return selectByXPath($array, $path, true, 0);
}

// get an element of the array by XPath
function getNodeValue(&$array, $path)
{
  return (getByXPath($array, $path));
}

// inserts the $node into the $array
function setNodeValue(&$array, $path, $node)
{
  eval('$array'.XPath2Index($path).'=$node;');
}

// removes the $node into the $array
function removeNode(&$array, $path)
{
  $exec = 'unset($array'.XPath2Index($path).');';
  eval($exec);
}

function getRootTagName(&$array)
{
  if (is_array($array))
    foreach ($array as $ak => $av)
      return ($ak);
}

function getFieldList(&$instance, $path)
{
  $result = array();
  if (substr($path, -1)!='/') $path .= '/';
  $exec = '$iresult = $instance'.XPath2Index($path).';';
  eval($exec);
  if (is_array($iresult))
    foreach ($iresult as $ne => $entry)
    {
      $result[$ne] = $entry[0]['$'];
    }
  return ($result);
}

function hasUniFormValues(&$instance, $value)
{
  $result = true;
  $value = trim(strtolower($value));
  foreach ($instance as $k => $v)
  {
    if (strtolower(trim($v))!=$value) $result = false;
  }
  return ($result);
}

?>
